<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputhidden} function plugin
 *
 * File:       function.pp_inputhidden.php<br>
 * Type:       function<br>
 * Name:       pp_inputhidden<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_inputhidden values=$ids output=$names}
 * {pp_inputhidden values=$ids name='box' separator='<br>' output=$names}
 * {pp_inputhidden values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_inputhidden}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputhidden($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

	$name = NULL;
	$value = NULL;
	$text = NULL;
	$eid = NULL;
	$extra = NULL;
	
	$except = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'name':
				$$_key = (string)$_val;
				break;
			
			case 'value':
				$$_key = (string)$_val;
				break;
			
			case 'text':
				$$_key = (string)$_val;
				break;
			case 'eid':
				$$_key = (string)$_val;
				break;
			
			case 'except':
				if( is_string($_val) && strstr($_val, ',') !== false ) {
					$$_key = explode(',', $_val);
				} else {
					$$_key = (array)$_val;
				}
				break;
			
			case 'assign':
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_inputhidden: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_inputhidden_output($name, $value, $eid, $except, $extra);

	return $_html_result;
}

function smarty_function_pp_inputhidden_output($name, $value, $eid, $except, $extra) {
	$_output = '';
	
	//if( isset($name) && isset($_REQUEST[$name]) ) {
	//	$value = (string)$_REQUEST[$name];
	//}
	
	if( isset($eid) ) {
		//if( isset($_REQUEST[$name]) ) {
		//	$value = (string)$_REQUEST[$name];
		//}
		if( strstr($name, '[]') === false ) {
			if( isset($_REQUEST[$name]) ) {
				$value = (string)$_REQUEST[$name];
			}
		} else {
			$nameb = str_replace('[]', '', $name);
			$name = str_replace('[]', '[a' . $eid . ']', $name);
			if( isset($_REQUEST[$nameb]['a' . $eid]) ) {
				$value = (string)$_REQUEST[$nameb]['a' . $eid];
			}
		}
	} else {
		//if( isset($_REQUEST[$name]) ) {
		//	$value = (string)$_REQUEST[$name];
		//}
		if( strstr($name, '[]') === false ) {
			if( isset($_REQUEST[$name]) ) {
				$value = (string)$_REQUEST[$name];
			}
		} else {
			$nameb = str_replace('[]', '', $name);
			if( !isset($_REQUEST[$nameb . '_index']) ) {
				$_REQUEST[$nameb . '_index'] = 0;
			}
			if( isset($_REQUEST[$nameb][$_REQUEST[$nameb . '_index']]) ) {
				$value = (string)$_REQUEST[$nameb][$_REQUEST[$nameb . '_index']];
			}
			$_REQUEST[$nameb . '_index']++;
		}
	}
	
	if( isset($except) ) {
		if( in_array($name, $except) ) {
			$_output .= '';
		} elseif( isset($name) ) {
			$_output .= '<input type="hidden" name="' . smarty_function_escape_special_chars($name) . '" value="' . smarty_function_escape_special_chars($value) . '"';
			$_output .= $extra . '>';
		}
		if( isset($_GET) && is_array($_GET) ) {
			foreach($_GET as $k => $v) {
				if( !in_array($k, $except) ) {
					$_output .= '<input type="hidden" name="' . smarty_function_escape_special_chars($k) . '" value="' . smarty_function_escape_special_chars($v) . '">';
				}
			}
		}
	} elseif( isset($name) ) {
		$_output .= '<input type="hidden" name="' . smarty_function_escape_special_chars($name) . '" value="' . smarty_function_escape_special_chars($value) . '"';
		$_output .= $extra . '>';
	} else {
		$_output .= '<input type="hidden" value="' . smarty_function_escape_special_chars($value) . '"';
		$_output .= $extra . '>';
	}
	
	return $_output;
}
//
?>